<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新分类</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="4">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="name">
                  <div>名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4" hidden>
                <el-form-item prop="workshop_id">
                  <div>车间</div>
                  <v-select v-model="data.workshop_id" action="workshopList" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="cost_id">
                  <div>成本中心</div>
                  <v-select v-model="data.cost_id" action="costList" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="org">
                  <div>组织</div>
                  <v-select v-model="data.org" action="orgList" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="department">
                  <div>部门</div>
                  <v-select v-model="data.department" :disabled="!data.org" :query="{org:data.org}" action="departmentList" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="status">
                  <div>状态</div>
                  <el-select v-model="data.status" size="mini">
                    <el-option v-for="(item, index) in theStatus" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="type">
                  <div>类型</div>
                  <el-select v-model="data.type" size="mini">
                    <el-option v-for="(item, index) in typeCategory" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="parent_id">
                  <div>上级分类</div>
                  <v-select v-model="data.parent_id" action="eqCategoryList" :is-top="true" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="admin_id">
                  <div>维护负责人</div>
                  <v-select v-model="data.admin_id" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>电话</div>
                  <el-input v-model="data.admin.phone" :disabled="true" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="其他负责人">
              <el-table :data="list">
                <el-table-column prop="admin.name" label="员工" min-width="16%" />
                <el-table-column prop="admin.phone" label="电话" min-width="16%" />
                <el-table-column prop="admin.phone2" label="电话2" min-width="16%" />
                <el-table-column prop="admin.email" label="邮箱" min-width="25%" />
                <el-table-column prop="weight" label="重要性" min-width="16%" :formatter="weightFormat" />
                <el-table-column min-width="8%" label="操作">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="otherAdminDel(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-row :gutter="10">
                    <el-form ref="append_form" :model="data" inline class="hf_form append_other_admin" :rules="appendRules">
                      <el-col :span="4">
                        <el-form-item prop="admin_id">
                          <v-select v-model="data.admin_id" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="4">
                        <el-form-item>
                          <el-input :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="4">
                        <el-form-item>
                          <el-input :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item>
                          <el-input :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="4">
                        <el-form-item prop="weight">
                          <el-select v-model="data.weight" size="mini">
                            <el-option v-for="(item, index) in weight" :key="index" :value="index" :label="item" />
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="append" />
                        </el-form-item>
                      </el-col>
                    </el-form>
                  </el-row>
                </div>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row :gutter="10">
                <el-col :span="4">
                  <div>
                    <span>创建时间日期</span>
                    <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>创建人</span>
                    <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div v-if="data.update_time">
                    <div>
                      <span>最近更新</span>
                      <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                    </div>
                    <div>
                      <span>更新者</span>
                      <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                    </div>
                  </div>
                </el-col>
                <el-col :span="20">
                  <wangeditor id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/asset/category'
import { typeCategory, theStatus, weight } from '@/utils/options'
import Wangeditor from '@/components/editor/wangeditor'
import VSelect from '@/components/select'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendOtherAdmin', 'delOtherAdmin', 'getWeightFormat'],
  components: {
    Wangeditor,
    VSelect
  },
  data() {
    return {
      list: [
        {
          'admin': {
            'name': '',
            'phone': '',
            'phone2': '',
            'email': ''
          }
        }
      ],
      data: {
        'admin': {
          'phone': ''
        },
        'cr_admin': {
          'name': ''
        }
      },
      typeCategory,
      theStatus,
      weight,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ],
        cost_id: [
          {
            required: true,
            message: '请选择成本中心',
            trigger: 'change'
          }
        ],
        org: [
          {
            required: true,
            message: '请选择组织',
            trigger: 'change'
          }
        ],
        department: [
          {
            required: true,
            message: '请选择部门',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'change'
          }
        ],
        parent_id: [
          {
            required: true,
            message: '请选择上级分类',
            trigger: 'change'
          }
        ],
        admin_id: [
          {
            required: true,
            message: '请选择维护负责人',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      appendRules: {
        admin_id: [
          {
            required: true,
            message: '请选择员工',
            trigger: 'change'
          }
        ],
        weight: [
          {
            required: true,
            message: '请选择重要性',
            trigger: 'change'
          }
        ]
      }
    }
  },
  watch: {},
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 92404) {
        this.$alert('您请求的分类不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.list = response.data.other_admin
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.append_form
      const admin_data = {
        'admin_id': this.data.admin_id,
        'category': this.$route.params.id,
        'weight': this.data.weight
      }
      const errorCode = 92004
      const errorMessage = '该人员已存在'
      this.appendOtherAdmin(API, thisForm, admin_data, errorCode, errorMessage)
    },
    otherAdminDel(id) {
      const admin_data = {
        'id': id
      }
      this.delOtherAdmin(API, id, admin_data)
    },
    weightFormat(row) {
      return this.getWeightFormat(row)
    }
  }
}
</script>
